<template>
    <div class="warpper-box">
      <div class="inner_box">
       <form-create 
       v-model="params.fApi" 
       :rule="params.rule" 
       :option="params.option" 
       @btn-search-click="loadList(true)" 
       @btn-add-click="add"
       @btn-reset-click="reset"/>
       <el-tabs v-model="activeName" type="card" @tab-click="handleClick">
        <el-tab-pane v-for="(item,index) in tab" :key="index" :label="item.label" :name="item.value">
          <label slot="label">
            <span style="cursor: pointer;">{{item.label}}</span>
          </label>
          <purchase-list
          :key="keys"
          :tableData="tables.tableData" 
          :tableLabel="tables.tableLabel" 
          :models="tables"
          @on-sendPurchase-click="sendPurchase"
          @on-selection-click="selectionChange"
          @on-search-click="loadList"/>
        </el-tab-pane>
      </el-tabs>
     
      <el-dialog :title="verify.invitation.title" :visible.sync="verify.invitation.visible" :width="verify.width" :showClose="false" :close-on-click-modal="false">
        <invitationAdd v-if="verify.hackReset" :row="verify.row"  @on-cancel-click="onCancel"/>
      </el-dialog>

      <el-dialog :title="verify.overt.title" :visible.sync="verify.overt.visible" :width="verify.width" :showClose="false" :close-on-click-modal="false">
        <overtAdd v-if="verify.hackReset" :row="verify.row"  @on-cancel-click="onCancel"/>
      </el-dialog>
        
      </div>
    </div>
  </template>
  
  <script>
  import store from '@/store'
  import { arrayFormat } from "@/utils/index"
  import { 
    PARAMS_PURCHASE_SEND,
    LABEL_LABOUR_PURCHASE_SEND,
    PURCHASE_SEND_TAB,
    OPTION_INLINE
  } from "@/views/labourPurchase/labourModel/purchase_form"
  import PurchaseList from "@/views/labourPurchase/labourModel/purchase_send_model_list"
  import invitationAdd from "@/views/labourPurchase/labourSend/invitation_add"
  import overtAdd from "@/views/labourPurchase/labourSend/overt_add"
  import {bidLabourPurchaseList} from "@/api/labour_purchase_api"
  export default {
    components:{
        PurchaseList,
        invitationAdd,
        overtAdd
    },
    data() {
      return {
        keys:0,
        activeName:"2",
        tab:PURCHASE_SEND_TAB,
        params:{
          fApi:{},
          rule:PARAMS_PURCHASE_SEND,
          option:OPTION_INLINE
        },
        tables:{
          total:0,
          fixed:'right',
          width:'200px',
          purchaseSendCheck:1,//主要处理待发起列表check根据状态判断禁止点击问题 防止其他列表串数据
          tableData:[],
          tableLabel: LABEL_LABOUR_PURCHASE_SEND
        },
        page:{
          purchaseType:"2",
          pageNum:1,
          pageSize:10,
        },
        verify:{
          row:'',
          width:"1200px",
          hackReset:true,
          invitation:{
            title:'邀请招标发起',
            visible:false,
          },
          overt:{
            title:'公开招标发起',
            visible:false,
          }
        },
        selectItem:[]
      };
    },
    mounted() {
      this.loadList(true);
      this.$nextTick(()=>{
        this.params.fApi.setValue({"time":""});
      });
    },
    methods: {
      //列表
      async loadList(formData) {
        let params = this.params.fApi.bind();
        if(params.time && params.time.length != 0){
          params.startTime = params.time[0];
          params.endTime = params.time[1];
        }
        if(formData !== undefined && formData.type === 'page'){
          this.page.pageNum = formData.pageNum;
          this.page.pageSize = formData.pageSize;
        }
        if(formData===true){
          this.page.pageNum = 1;
          this.keys++;
        }
        params.purchaseType = this.page.purchaseType;
        params.pageNum = this.page.pageNum;
        params.pageSize = this.page.pageSize;
        const req = await bidLabourPurchaseList(params);
        if(req.code === 200){
          this.tables.tableData = req.data.list;
          let btn = this.$store.getters.permission_btns;
          if(btn.includes('labourPurchaseBatchSend')){//批量新增采购任务
            this.params.fApi.hidden(false,'add')
          }else{
            this.params.fApi.hidden(true,'add')
          }
          this.tables.tableData.forEach(v=>{
            if(btn.includes('labourPurchaseSingleSend')){//是否有权限
              if(v.applyNum !== v.purchasedNum){// 如果申请数量==已采购数量 就不能发起采购了
                v.sendPurchase = true;
              }
            }
          })
          this.tables.total = req.data.total;
        }
      },
      //切换
      handleClick(obj){
        this.page.purchaseType = obj.name;
        this.loadList(true);
      },
      //获取选中id
      selectionChange(arr){
        this.selectItem = arr;
      },
      //发起
      add(){
        if(this.selectItem.length === 0){
          return this.$message.error('请勾选数据');
        }
        //判断项目名称是否不一样
        const arr = this.selectItem;
        for (var i = 0; i < arr.length - 1; i++) {
          for (var j = i + 1; j < arr.length; j++) {
            if (arr[i].name !== arr[j].name) {
              this.$message.error('多个劳务采购发起时，项目名称需要相同！');
              return false
            }
          }
        }
        this.verify.row = this.selectItem;
        //更新页面
        this.verify.hackReset = false;
        this.$nextTick(()=>{
          this.verify.hackReset = true;
        });
        let flag = this.page.purchaseType === '2' ? 'invitation':'overt';
        this.verify[flag].visible = true;
      },
      //发起
      sendPurchase(row){
        this.verify.row = row;
        this.verify.title = row.purchaseType === '2' ? '邀请招标':'公开招标';
        this.verify.hackReset = false;
        this.$nextTick(()=>{
          this.verify.hackReset = true;
        });
        let flag = row.purchaseType === '2' ? 'invitation':'overt';
        this.verify[flag].visible = true;
      },
      reset(){
        this.params.fApi.resetFields();
        this.loadList(true);
      },
      onCancel(flag){
        this.verify[flag].visible = false;
        this.selectItem = [];
        this.loadList(true);
      }
    }
  };
  </script>